<template>
  <div class="register-page">
    <van-nav-bar
      title="注册"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="请输入账号"
        :rules="[
        { required: true, message: '请填写用户名' },
        {pattern:/\w{5,}/,message:'用户名不能小于5位'}
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[
        { required: true, message: '请填写密码' },
        {pattern:/\w{6,}/,message:'密码不能小于6位'}
        ]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>
    <p class="bottom">
      <router-link class="link left" to="/index">点我回首页</router-link>
    <router-link class="link right" to="/login">已有账号，去登陆~</router-link>
    </p>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'RegisterPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const data = await register(values)
      // console.log(data.description)
      if (data.status !== 200) {
        this.$toast(data.description)
      } else {
        this.$toast('注册成功')
        setTimeout(() => {
          this.$router.push('/login')
        }, 300)
      }
    },
    onClickLeft () {
      this.$toast('返回')
      this.$router.back()
    }
  }
}
</script>

<style lang="less" scoped>
.bottom{
  width: 100%;
  height: 50px;
  text-align: center;
  .link {
    color: #666;
    font-size: 14px;
    padding-right: 7px;
  }
}

</style>
